package org.ifilm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ifilm.model.User;
import org.ifilm.service.UserService;
import org.ifilm.util.GeneralConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * User Contoller. All requests about users will be here
 * @author Mustafa Genç
 *
 */
@Controller
@RequestMapping("/user/**")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/register", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView createUser(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView();
		
		User user = new User();
		user.setName(request.getParameter(GeneralConstants.userName));
		user.setSurname(request.getParameter(GeneralConstants.userSurname));
		user.setEmail(request.getParameter(GeneralConstants.userEmail));
		user.setUserType(request.getParameter(GeneralConstants.userType));
		//TODO : password should be encrypted
		user.setPassword(request.getParameter(GeneralConstants.userPassword));
		
		//TODO : also login??
		User createdUser = userService.createUser(user);
		
		if (createdUser != null) {
			request.setAttribute(GeneralConstants.user, createdUser);
			result.setViewName("/index.jsp");
		} else {
			result.setViewName("/registerUser.jsp");
		}
		
		return result;
	}
	
	@RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView();
		
		HttpSession session =  request.getSession();
		
		String email = request.getParameter(GeneralConstants.userEmail);
		String password = request.getParameter(GeneralConstants.userPassword);
		
		User loggedUser = userService.getUser(email, password);
		
		if (loggedUser != null) {
			session.setAttribute(GeneralConstants.user, loggedUser);
			result.setViewName("/project/listProjects.do?page=1&max=" + GeneralConstants.MAXIMUM_PROJECT_PAGINATION);
		} else {
			result.setViewName("/index.jsp");
		}
		
		return result;
	}
	
	@RequestMapping(value = "/logout", method = {RequestMethod.POST, RequestMethod.GET})
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		
		session.invalidate();
	}
	
	//TODO
	@RequestMapping(value = "/getUser", method = {RequestMethod.POST, RequestMethod.GET})
	public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession();
		response.sendRedirect("/film/profile.jsp");
//		request.getRequestDispatcher("/profile.jsp").include(request, response);
	}
}
